define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                search: false,
                //导出下拉列表选项
                exportTypes: ['csv','excel'],
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'group_id', title: __('Group'),formatter: Table.api.formatter.status,searchList: {1: __('OrdinaryUsers'), 2: __('Blogger')}},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'money', title: __('Money'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'blogger_goods',
                                    title: __('BloggerGoods'),
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'user/user/blogger_goods',
                                    visible: function (row) {
                                        // 自定义按钮 动态是否显示
                                        if (row.group_id == 1) {
                                            return false;
                                        }
                                        return true;
                                    }
                                },
                                {
                                    name: 'up_money',
                                    title: __('AccountModification'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'user/user/up_money'
                                }
                            ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        up_money: function () {
            Controller.api.bindevent();
        },
        blogger_goods: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    up_rate_url: 'user/user_goods/edit',
                }
            });

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.size() > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'user/user/table1/ids/'+ Config.ids + location.search,
                    pk: 'goods_id',
                    toolbar: '#toolbar1',
                    sortName: 'user_goods.id',
                    search: false,
                    //试图切换
                    showToggle: false,
                    //字段显示控制
                    showColumns: false,
                    //显示导出按钮
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'goods.goods_id', title: __('Goods_id')},
                            {field: 'goods.goods_name', title: __('Goods_name')},
                            {field: 'goods.image', title: __('GoodsImg'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                            {field: 'blogger_rate', title: __('BloggerRate'), operate: false,editable:true},
                            {field: 'createtime', title: __('Createtime'), operate: false, addclass: 'datetimerange', formatter: Table.api.formatter.datetime},
                            {
                                field: 'operate',
                                width: '140px',
                                title: __('Operate'),
                                table: table1,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'BatchDeletion',
                                        text: __('Del'),
                                        classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                        icon: 'fa fa-times',
                                        url: 'user/user_goods/del',
                                        refresh: true
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ],
                    onEditableSave:function (field, row, oldValue, $el) {
                        var data = {};  //组合更新数据
                        data["row[field]"] =field;  //字段名
                        data["row[" + field + "]"] = row[field];    //字段值
                        data["row[id]"] = row['id'];    //主键
                        Fast.api.ajax({
                            url: $.fn.bootstrapTable.defaults.extend.up_rate_url + "/ids/" + row[this.pk],
                            data: data
                        },function () {
                            $(".btn-refresh").trigger("click");
                        });
                    }
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                //在普通搜索渲染后
                table2.on('post-common-search.bs.table', function (event, table) {
                    var form = $("form", table.$commonsearch);
                    $("input[name='category_id']", form).addClass("selectpage").data("source", "litestore/litestorecategory/index").data("primaryKey", "id").data("field", "name").data("orderBy", "weigh desc");
                    Form.events.cxselect(form);
                    Form.events.selectpage(form);
                });
                table2.bootstrapTable({
                    url: 'user/user/table2/ids/'+ Config.ids+ location.search,
                    pk: 'goods_id',
                    toolbar: '#toolbar2',
                    sortName: 'goods_id',
                    search: false,
                    //试图切换
                    showToggle: false,
                    //字段显示控制
                    showColumns: true,
                    //显示导出按钮
                    showExport: false,
                    columns: [
                        [
                            {checkbox: true,formatter:function(value, row, index){
                                if (row.choice==10){
                                    return {disabled: true};
                                }
                            }},
                            {field: 'goods_id', title: __('Goods_id')},
                            {field: 'goods_name', title: __('Goods_name')},
                            {field: 'pro_no', title: __('Pro_no'), operate: 'LIKE'},
                            {field: 'name_p', title: __('CategoryName'),visible: false,operate: false},
                            {field: 'category_id', visible:false,title: __('Category_id')},
                            {field: 'image', title: __('GoodsImg'),operate: false, formatter: Table.api.formatter.image},
                            {field: 'goods_status', title: __('Goods_status'), searchList: {"10":__('On sale'),"20":__('Goods removed')}, formatter: Table.api.formatter.status},
                            {field: 'choice', title: __('Choice'), searchList: {"10":__('Selected'),"20":__('NotSelected')},operate: false,
                                formatter:function (value, row, index) {
                                    if(value == 10){
                                        return __('Selected');
                                    }else{
                                        return __('NotSelected');
                                    }
                                },cellStyle: function (value, row, index) {
                                    if (value == 10) {
                                        return{
                                            css:{color:'blue'}
                                        }
                                    }else{
                                        return{
                                            css:{color:'red'}
                                        }
                                    }
                                }
                            },
                            {
                                field: 'operate',
                                width: '140px',
                                title: __('Operate'),
                                table: table2,
                                events: Table.api.events.operate,
                                buttons: [
                                    {
                                        name: 'BatchAdd',
                                        text: __('Add'),
                                        classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                        icon: 'fa fa-plus',
                                        url: 'user/user_goods/batch_add/user_id/'+Config.ids,
                                        refresh: true,
                                        visible: function (row) {
                                            // 自定义按钮 动态是否显示
                                            if (row.choice == 10) {
                                                return false;
                                            }
                                            return true;
                                        }
                                    }
                                ],
                                formatter: Table.api.formatter.operate
                            }
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});